/*
 * $Id$
 *
 * Copyright ( c ) 2014 XuNuo Corporation. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of XuNuo
 * Corporation ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with XuNuo Corporation or a XuNuo
 * authorized reseller (the "License Agreement"). XuNuo may make changes to the
 * Confidential Information from time to time. Such Confidential Information may
 * contain errors.
 *
 * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, XuNuo DISCLAIMS ALL
 * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH
 * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR
 * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
 * PARTICULAR PURPOSE. XuNuo DOES NOT WARRANT THAT END USER'S USE OF THE
 * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE.
 *
 * XuNuo SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR
 * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL
 * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN
 * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF XuNuo HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES. XuNuo' TOTAL LIABILITY TO END USER SHALL NOT
 * EXCEED THE AMOUNTS PAID FOR THE XuNuo SOFTWARE BY END USER DURING THE PRIOR
 * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE.  BECAUSE SOME
 * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER.
 *
 * Copyright version 2.0
 */
package com.th.supcom.pds.prescstatusquery.client.widget;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.widget.Component;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.pds.common.client.commom.PdsAuditPrescServiceFactory;
import com.th.supcom.pds.common.client.commom.SimpleMethod;
import com.th.supcom.pds.common.client.model.PdsSupplyDetailModel;
import com.th.supcom.pds.common.client.model.PdsSupplyModel;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.internal.client.model.PatientInfoModel;
import com.th.supcom.pds.outdrugreturnapply.client.common.PdsSupplyResultModel;
import com.th.supcom.pds.prescstatusquery.client.service.SupplyStatusQueryRpcAsync;

/**
 * 处方状态查询Action
 * 
 * @author 杨冲
 */
public class SupplyStatusQueryAction
{
    private static final SupplyStatusQueryRpcAsync S_SUPPLY_STATUS_QUERY_RPC_ASYNC = PdsAuditPrescServiceFactory.getSupplyStatusQueryService ();
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";
    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    private SupplyStatusQueryDetailGrid m_prescStatusQueryDetailGrid;

    private SupplyStatusQueryFields m_prescStatusQueryFields;

    private SupplyStatusQueryPrescGrid m_prescStatusQueryPrescGrid;
    private List <PdsSupplyResultModel> m_pdsSupplyResultModels;
    private SupplyStatusQueryPanel m_supplyStatusQueryPanel;

    /**
     * 构造函数
     */
    public SupplyStatusQueryAction ()
    {

    }

    /**
     * 清除 flag ture:全部清空 flase:不清除输入框
     */
    public void clear (Boolean flag)
    {
        if (flag == Boolean.TRUE)
        {
            m_prescStatusQueryFields.getRecipeNoField ().clear ();
            m_prescStatusQueryFields.getConsumeProofField ().clear ();
        }
        m_prescStatusQueryFields.getPrescSource ().clear ();
        m_prescStatusQueryFields.getBirthDateField ().clear ();
        m_prescStatusQueryFields.getSexField ().clear ();
        m_prescStatusQueryFields.getNameField ().clear ();
        m_prescStatusQueryFields.getPatientIdField ().clear ();
        m_prescStatusQueryFields.getBalanceField ().clear ();
        m_prescStatusQueryFields.getVisitTypeNameField ().clear ();
        m_prescStatusQueryFields.getVisitIdField ().clear ();
        m_prescStatusQueryFields.getVisitBeginDateField ().clear ();
        m_prescStatusQueryFields.getDeptNameField ().clear ();
        m_prescStatusQueryFields.getDay ().clear ();
        m_prescStatusQueryFields.getNumber ().clear ();

        m_prescStatusQueryPrescGrid.clear ();
        m_prescStatusQueryDetailGrid.clear ();

        m_prescStatusQueryFields.getConsumeProofField ().focus ();
    }

    private void initPatientInfo (PatientInfoModel model)
    {

        if (model.getSex () != null)
        {
            m_prescStatusQueryFields.getSexField ().setValue (model.getSex ());
        }

        m_prescStatusQueryFields.getNameField ().setValue (model.getName ());
        m_prescStatusQueryFields.getPatientIdField ().setValue (model.getPatiId ());
        m_prescStatusQueryFields.getBirthDateField ().setValue (SimpleMethod.formatDate (model.getBirthDate ()));

        m_prescStatusQueryFields.getBalanceField ().setValue (model.getBalance ());
        final DateTimeFormat format = DateTimeFormat.getFormat ("yyyy-MM-dd");
        if (model.getVisitId () != null)
        {
            m_prescStatusQueryFields.getVisitIdField ().setValue (model.getVisitId () + "");
        }
        if (model.getVisitBeginDates () != null)
        {
            m_prescStatusQueryFields.getVisitBeginDateField ().setValue (format.format (model.getVisitBeginDates ()));
        }
        m_prescStatusQueryFields.getDeptNameField ().setValue (model.getDeptName ());
        if (model.getVisitDay () != null)
        {
            m_prescStatusQueryFields.getDay ().setValue (model.getVisitDay () + "");
        }
        m_prescStatusQueryFields.getNumber ().setValue (model.getBedCode ());
    }

    /**
     * 根据条件查询处方明细
     */
    public void findPrescByCondition (String rcptNo, String prescNo)
    {
        final Component focusComponent = rcptNo == null ? m_prescStatusQueryFields.getConsumeProofField () : m_prescStatusQueryFields.getRecipeNoField ();
        if (rcptNo == null && prescNo == null)
        {
            ClientUtils.alertMessageBox ("处方号或收据号不能同时为空！请重新输入！", focusComponent);
            return;
        }
        Map <String, Object> conditionMap = new HashMap <String, Object> ();
        conditionMap.put ("rcptNo", rcptNo);
        conditionMap.put ("prescNo", prescNo);
        mask ();
        S_SUPPLY_STATUS_QUERY_RPC_ASYNC.findSupplyStatusInfo (conditionMap, new AsyncCallback <List <PdsSupplyResultModel>> ()
        {

            @Override
            public void onSuccess (List <PdsSupplyResultModel> result)
            {
                clear (false);
                m_pdsSupplyResultModels = result;
                initPatientInfo (result.get (0).getPatientInfoModel ());
                m_prescStatusQueryFields.getPrescSource ().setValue (result.get (0).getPdsSupplyModel ().getPrescSourceName ());
                for (PdsSupplyResultModel pdsSupplyResultModel : result)
                {
                    m_prescStatusQueryPrescGrid.getGrid ().getListStore ().add (pdsSupplyResultModel.getPdsSupplyProModel ());
                }
                m_prescStatusQueryPrescGrid.selectRow (0);
                unmask ();

            }

            @Override
            public void onFailure (Throwable caught)
            {
                clear (true);
                unmask ();
                ClientUtils.alertMessageBox (caught.getMessage (), focusComponent);
            }
        });
    }

    /**
     * 当点击主记录表格处方时 请领明细表格相应弹出或隐藏内容
     * 
     * @param selectedModel
     */
    public void doSelectedChange (PdsSupplyModel selectedModel)
    {
        if (selectedModel == null)
        {
            m_prescStatusQueryDetailGrid.clear ();
            return;
        }
        if (m_pdsSupplyResultModels == null || m_pdsSupplyResultModels.isEmpty ())
        {
            return;
        }
        final List <PdsSupplyDetailModel> applyDetails = getDetailSelected (selectedModel, m_pdsSupplyResultModels);
        m_prescStatusQueryDetailGrid.addResult (applyDetails);
    }

    /**
     * 获得选中主记录对应的所有明细记录
     * 
     * @return
     */
    private List <PdsSupplyDetailModel> getDetailSelected (final PdsSupplyModel selectModel, final List <PdsSupplyResultModel> modelList)
    {
        for (PdsSupplyResultModel pdsSupplyResultModel : modelList)
        {
            if (selectModel.getPrescNo ().equals (pdsSupplyResultModel.getPdsSupplyModel ().getPrescNo ()))
            {
                return pdsSupplyResultModel.getPdsSupplyDetailModels ();
            }
        }
        return null;
    }

    private void mask ()
    {
        m_supplyStatusQueryPanel.mask ("正在为你加载……");
    }

    private void unmask ()
    {
        m_supplyStatusQueryPanel.unmask ();
    }

    /**
     * 初始化焦点事件
     */
    public void initFocus ()
    {
        m_prescStatusQueryFields.getConsumeProofField ().focus ();
    }

    public void setPrescStatusQueryDetailGrid (final SupplyStatusQueryDetailGrid prescStatusQueryDetailGrid)
    {
        m_prescStatusQueryDetailGrid = prescStatusQueryDetailGrid;
    }

    public void setPrescStatusQueryFields (final SupplyStatusQueryFields prescStatusQueryFields)
    {
        m_prescStatusQueryFields = prescStatusQueryFields;
    }

    public void setPrescStatusQueryPrescGrid (final SupplyStatusQueryPrescGrid prescStatusQueryPrescGrid)
    {
        m_prescStatusQueryPrescGrid = prescStatusQueryPrescGrid;
    }

    public void setSupplyStatusQueryPanel (SupplyStatusQueryPanel supplyStatusQueryPanel)
    {
        m_supplyStatusQueryPanel = supplyStatusQueryPanel;
    }

}
